﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using V5_EaspLibs.DBUtility;
using System.Data.OleDb;
using System.Data.OracleClient;
namespace Dapper {
    /// <summary>
    /// 
    /// </summary>
    public class DbHelperDapper {
        /// <summary>
        /// 
        /// </summary>
        public static string connectionString = PubConstant.ConnectionString;
        /// <summary>
        /// 
        /// </summary>
        public DbHelperDapper() {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static IDbConnection GetOpenConnection() {
            connectionString = string.Format(connectionString, AppDomain.CurrentDomain.BaseDirectory + "\\");
            IDbConnection connection = null;
            switch (PubConstant.ConnectionDbType) {
                case DataBaseType.OleDb:
                    connection = new OleDbConnection(connectionString);
                    break;
                case DataBaseType.SqlServer:
                    connection = new SqlConnection(connectionString);
                    break;
                case DataBaseType.Oracle:
                    connection = new OracleConnection(connectionString);
                    break;
                //case DataBaseType.MySql:
                //    connection = new MySqlConnection(connectionString);
                //    break;
                //case DataBaseType.SQLite:
                //    connection = new SQLiteConnection(connectionString);
                //    break;
            }
            connection.Open();
            return connection;
        }

        /// <summary>
        /// 使用dapper执行数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int Execute(string sql, object param) {
            using (var conn = GetOpenConnection()) {
                try {
                    return conn.Execute(sql, param);
                }
                catch (Exception ex) {
                    throw new Exception(ex.Message);
                }
            }
        }
        /// <summary>
        ///  使用dapper获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static IEnumerable<T> Query<T>(string sql, object param) {
            using (var conn = GetOpenConnection()) {
                try {
                    return conn.Query<T>(sql, param);
                }
                catch (SqlException ex) {
                    throw new Exception(ex.Message);
                }
            }
        }
        /// <summary>
        /// 获取dapper组串
        /// </summary>
        /// <param name="ColumnName"></param>
        /// <param name="ColumnType"></param>
        /// <returns></returns>
        public static string GetColumnString(string ColumnName, string ColumnValue, string ColumnType) {
            string Temp = string.Empty;
            switch (PubConstant.ConnectionDbType) {
                case DataBaseType.OleDb:
                    if (ColumnType.ToLower() != "int") {
                        Temp = "'" + ColumnValue + "'";
                    }
                    else {
                        Temp = " " + ColumnValue + "";
                    }
                    break;
                case DataBaseType.SqlServer:
                    Temp = "@" + ColumnName + "";
                    break;
                case DataBaseType.Oracle:
                    Temp = "@" + ColumnName + "";
                    break;
                case DataBaseType.MySql:
                    Temp = "@" + ColumnName + "";
                    break;
                case DataBaseType.SQLite:
                    Temp = "@" + ColumnName + "";
                    break;
            }
            return Temp;
        }

        public static DataSet Query(string SQL) {
            DataSet ds = new DataSet();
            switch (PubConstant.ConnectionDbType) {
                case DataBaseType.OleDb:
                    ds = DbHelperOleDb.Query(SQL);
                    break;
                case DataBaseType.SqlServer:
                    ds = DbHelperSQL.Query(SQL);
                    break;
                case DataBaseType.Oracle:
                    //ds = DbHelperOracle.Query(SQL);
                    break;
                //case DataBaseType.MySql:
                //    ds = DbHelperMySQL.Query(SQL);
                //    break;
                //case DataBaseType.SQLite:
                //    ds = DbHelperSQLite.Query(SQL);
                //    break;
            }
            return ds;
        }
    }
}
